package cn.teleus.admin.controller;

import cn.teleus.admin.common.ControllerCode;
import cn.teleus.admin.entity.User;
import cn.teleus.admin.fegin.TeleFeginClient;
import cn.teleus.admin.service.UserService;
import cn.teleus.common.util.Root;
import cn.teleus.common.util.page.PageQuery;
import cn.teleus.common.util.page.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/api/v2/adminService")
public class UserController implements ControllerCode {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@Resource
	private TeleFeginClient teleFeginClient;

	/**
	 * 分页获取用户
	 * 
	 * @param searchInfo
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/users", method = RequestMethod.POST)
	public String getUsersBySearchInfo(@RequestParam(value = "searchInfo", required = false) String searchInfo,
			@RequestParam(value = "pageIndex") int pageIndex) {

		String uuid = teleFeginClient.weixin();
		PageQuery pageQuery = new PageQuery();
		pageQuery.setPageIndex(pageIndex);

		PageResult<User> pageResult = userService.getPageList(pageQuery);
		return Root.getRootOKAndSimpleMsg().setData(pageResult).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
	public String getUserById(@PathVariable("id") int id) {
		User user = userService.getById(id);
		user.setPassword(null);
		return Root.getRootOKAndSimpleMsg().setData(user).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
	public String deleteUserById(@PathVariable("id") int id) {
		boolean resultBoolean = userService.deleteById(id);
		if (resultBoolean) {
			return Root.getRootOKAndSimpleMsg().toJsonString();
		}
		return Root.getRootFailAndSimpleMsg().toJsonString();
	}
}
